<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.css" rel="stylesheet" />
		<link href="../css/header.css" rel="stylesheet" />
	</head>

	<body>
		<!-- 头部标题栏 -->
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-color"></a>
			<a id="link_submit_nickname" class="mui-icon mui-icon-right-nav mui-pull-right  title-color " style="font-size: 16px;  line-height: 25px;">完成</a>
			<h1 class="mui-title title-color" >设置昵称</h1>
		</header>
		<!-- 主体部分   -->
		<div class="mui-content">
			<div id="" class="mui-input-row">
				<input type="text"  id="nickname" class="mui-input-speech  mui-input-clear"  placeholder="修改昵称" />
			</div>
		</div>
		<script src="../js/app.js"></script>
		<script src="../js/mui.js"></script>
		<script type="text/javascript">
			mui.init();
			mui.plusReady(function () {
			    //获取全局用户对象
				var user = app.getUserGlobalInfo();
				//获取昵称
				var nickname=user.nickname;
				//获取文本框对象
				var txt_nickname=document.getElementById("nickname");
				//动态设置文本框的内容
				txt_nickname.value=nickname;
				//修改昵称
				var link_submit_nickname=document.getElementById("link_submit_nickname");
				link_submit_nickname.addEventListener("tap",function(){
					//获取修改后的昵称
					var newNickname=txt_nickname.value;
					if(!app.isNotNull(newNickname)){
						app.showToast("昵称不能为空","error");
						return false;
					}
					if(newNickname.length>10){
						app.showToast("昵称长度不能超过10位","error");
						return false;
					}
					plus.nativeUI.showWaiting("请稍后....");
					//异步请求
					mui.ajax(app.serverUrl+"user/setNickname",{
						data:{
							id: user.id,
							nickname: newNickname
						},
						type:'post',//HTTP请求类型
						timeout:10000,//超时时间设置为10秒；
						success:function(data){ 
							
							if(data.status==200){//若服务器中成功返回了非空的数据,则表明了账户与密码验证已经成功
							    plus.nativeUI.closeWaiting();
								var userInfo = data.data;//获取json对象中的用户对象
								txt_nickname.value=newNickname;
								app.setUserGlobalInfo(userInfo); //将用户对象存入本地内存
								//触发webview的另外一个自定义事件 更新用户昵称
								var mywebview=plus.webview.getWebviewById("wechat_my.html");
								mui.fire(mywebview,"refresh");
								//页面跳转
								mui.back();
							}else{//服务器返回的数据为空,表明账户与密码验证失败
								app.showToast(data.msg,"error");//弹出错误信息
							}
						},
						error:function(XMLHttpRequest, textStatus, errorThrown){
							if(textStatus=="timeout"){
								mui.toast("连接超时");//超过十秒报错
							}else if(textStatus=="error"){
								mui.toast("连接出错");
							}
						}
					});
					
				})
			})
		</script>
	</body>

</html>
